import api from './fb_http';
import Nprogress from 'nprogress'
// 获取广告账户
export let getAdaccounts = (limit = 1000, after) => {
  let param = {
    "fields": "name",
    limit
  }
  if (after) param.after = after;
  return api.get('/me/adaccounts', param).then(async (res) => {
    if (!res.data.length) return [];
    // 没有下一页
    if (!res.paging.next) {
      // console.log(data)
      return res.data;
    } else {
      // 获取剩下的
      return res.data.concat(
        await getAdaccounts(
          limit,
          res.paging.cursors.after
        )
      );
    }
  })
}

/**
 * 导出日常运营数据表
 * @param {String} id
 * @param {Object} param 请求参数
 * @param {String} start_time
 * @param {String} end_time
 * @param {String} after 下一页的标识
 */
export let getInsights = function (id, param, start_time, end_time, after = '') {
  Nprogress.start()
  param.time_range = `{since: '${start_time}', until: '${end_time}'}`;
  if (after) param.after = after;
  return api.get(`/${id}/insights`, param).then(async res => {
    if (!res.data.length) return [];
    // 没有下一页
    if (!res.paging.next) {
      // console.log(data)
      // if (store.state.loading) store.commit('setLoading', false)
      return res.data;
    } else {
      // if (!store.state.loading) store.commit('setLoading', true)
      // 获取剩下的
      return res.data.concat(
        await getInsights(
          id,
          param,
          start_time,
          end_time,
          res.paging.cursors.after
        )
      );
    }
  });
};

// 素材表单独获取创建时间
export let getAdsInsights = function (id, param, after = '') {
  if (after) param.after = after;
  return api.get(`/${id}/ads`, param).then(async res => {
    if (!res.data.length) return [];
    // 没有下一页
    if (!res.paging.next) {
      // console.log(data)
      return res.data;
    } else {
      // 获取剩下的
      return res.data.concat(
        await getAdsInsights(id, param, res.paging.cursors.after)
      );
      // return res.data
    }
  });
};

// 获取而外的数据和Insights 合并了getAdsInsights flag作为基本表单独的处理 有的话是account_id
export let getMoreInsights = function (url, param, after = '', flag = false) {
  Nprogress.start()
  if (after) param.after = after;
  return api.get(url, param).then(async res => {
    if (!res.data.length) return [];
    // 过滤没有成效的数据
    res.data = res.data.filter((i) => {
      return i.insights !== undefined
    })
    // 基本表做处理  应用程序层级流量限制 现在暂时下掉.
    if (flag) {
      for (let i = 0; i < res.data.length; i++) {
        if (res.data[i].adcreatives) res.data[i].material_name = await getMaterialName(res.data[i].adcreatives.data[0].object_story_spec, flag)
      }
    }
    // 没有下一页
    if (!res.paging.next) {
      Nprogress.done()
      // console.log(data)
      // if (store.state.loading) store.commit('setLoading', false)
      return res.data;
    } else {
      // if (!store.state.loading) store.commit('setLoading', true)
      // 获取剩下的
      return res.data.concat(
        await getMoreInsights(url, param, res.paging.cursors.after, flag)
      );
      // return res.data
    }
  });
};


// 获取账户的图库 hash 检索 id=>账户id
export let getAdimages = async function (id, hash) {
  // let param =
  // if (after) param.after = after;
  let res = await api.get(`/${id}/adimages`, {
    "fields": "name,permalink_url",
    "hashes": `['${hash}']`
  })
  return res.data[0].name
};

// 获取视频信息 id=>视频id
export let getVideo = async function (id, param) {
  // let param =
  // if (after) param.after = after;
  let res = await api.get(id, {
    "fields": "title"
  })
  return res.title
};
// 获取素材基本数据表中的素材名
let getMaterialName = async (res, id) => {
  if (res) {
    if (res.video_data) {
      return await getVideo(res.video_data.video_id)
    } else if (res.link_data) {
      let link_data = res.link_data
      // 图片组
      if (link_data.child_attachments) {
        let name = await getImage(link_data.child_attachments, id)
        return name
      } else {
        // 单张
        return await getAdimages(id, link_data.image_hash)
      }
    }
  }
}
// 取图片组信息-name
let getImage = async (res, id) => {
  Nprogress.start()
  let arr = []
  for (let i = 0; i < res.length; i++) {
    arr.push(await getAdimages(id, res[i].image_hash))
  }
  return arr.join('&&')
}